package com.test.dababy.dababy_00_customer_service;

import static com.test.dababy.dababy_00_utils.DababyConfigs.Debug;
import static com.test.dababy.dababy_00_utils.DababyUtil.startLoading;
import static com.test.dababy.dababy_00_utils.DababyUtil.stopLoading;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.dababy.R;
import com.test.dababy.dababy_10_vip.Pers_dababyonalIn_dababyfo;
import com.test.dababy.dababy_00_http.OkHttpUtil;

public class Feed_dababybackActivity extends AppCompatActivity {

    private EditText inputEt;
    private TextView countTv;

    private String type_dababy = "";

    public String a6l1="";
    public int s35a=226;
    public int fTj8=20;
    public float e51u=0.5838913f;
    public boolean s7DT=true;
    public String jFGd="";
    public char h0lx=' ';
    public boolean oZgB=false;

    public void oqBeA(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(a6l1))
                    setA6l1(String.valueOf(getS35a())+getfTj8()+getE51u()+isS7DT()+getjFGd()+getH0lx()+isoZgB());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setA6l1(String a6l1) {
        this.a6l1 = a6l1;
    }

    public void setS35a(int s35a) {
        this.s35a = s35a;
    }

    public void setfTj8(int fTj8) {
        this.fTj8 = fTj8;
    }

    public void setE51u(float e51u) {
        this.e51u = e51u;
    }

    public void setS7DT(boolean s7DT) {
        this.s7DT = s7DT;
    }

    public void setjFGd(String jFGd) {
        this.jFGd = jFGd;
    }

    public void setH0lx(char h0lx) {
        this.h0lx = h0lx;
    }

    public void setoZgB(boolean oZgB) {
        this.oZgB = oZgB;
    }

    public String getA6l1() {
        return a6l1;
    }

    public int getS35a() {
        return s35a;
    }

    public int getfTj8() {
        return fTj8;
    }

    public float getE51u() {
        return e51u;
    }

    public boolean isS7DT() {
        return s7DT;
    }

    public String getjFGd() {
        return jFGd;
    }

    public char getH0lx() {
        return h0lx;
    }

    public boolean isoZgB() {
        return oZgB;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dababy_feedback);
        if (!Debug) getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        inputEt = findViewById(R.id.adf_et_input);
        countTv = findViewById(R.id.adf_tv_count);

        findViewById(R.id.adf_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.adf_ib_cs).setOnClickListener(v -> new Customer_dababyServiceDialog(this).show());

        TextView[] typeTvs = new TextView[]{
            findViewById(R.id.adf_tv_t_pt),
            findViewById(R.id.adf_tv_t_ab),
            findViewById(R.id.adf_tv_t_sn),
            findViewById(R.id.adf_tv_t_or)
        };

        for (int i=0;i<typeTvs.length;i++){
            final int fi = i;
            typeTvs[i].setOnClickListener(v -> {
                for (int j=0;j<typeTvs.length;j++){
                    if (fi==j){

                        type_dababy = typeTvs[j].getText().toString();
                        OkHttpUtil.getInstance_dababy().uploadEventPoint("FEEDBACK", type_dababy.toUpperCase()+"_CLICK","");

                        typeTvs[j].setBackgroundResource(R.drawable.bg_main_rc12dp);
                        typeTvs[j].setTextColor(getColor(R.color.white));

                    }else {
                        typeTvs[j].setBackgroundResource(R.drawable.bg_dark_rc16dp_2);
                        typeTvs[j].setTextColor(Color.parseColor("#FF818283"));
                    }
                }
            });
        }

        inputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable s) {
                int length = s.length();
                countTv.setText(length+"/1000");
                if (length>=1000) countTv.setTextColor(Color.RED);
                else countTv.setTextColor(Color.parseColor("#FF9F9F9F"));
            }
        });

        inputEt.setOnFocusChangeListener((v, hasFocus) ->
            OkHttpUtil.getInstance_dababy().uploadEventPoint("FEEDBACK","INPUT",hasFocus ? "": inputEt.getText().toString().trim())
        );

        findViewById(R.id.adf_btn_submit).setOnClickListener(v -> {
            String feedback = inputEt.getText().toString().trim();
            if (TextUtils.isEmpty(type_dababy) || TextUtils.isEmpty(feedback)) {
                Toast.makeText(this,getString(R.string.feedback_input_title),Toast.LENGTH_SHORT).show();
                return;
            }
            startLoading(this,"");
            OkHttpUtil.getInstance_dababy().submitFb(type_dababy,feedback,this::onSubmitResponse_dababy);
        });

    }
    
    @Override
    protected void onResume() {
        super.onResume();
        oqBeA();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("FEEDBACK","START","");
    }

    @Override
    protected void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("FEEDBACK","END","");
    }


    public void onSubmitResponse_dababy(){

        long currentTime = System.currentTimeMillis();
        long lastFeedbackTime = Pers_dababyonalIn_dababyfo.getInstance().getLastFeedbackTime();

        if (currentTime - lastFeedbackTime > 604800000L){//时隔一周提交反馈经验+3
            //经验+3
            Pers_dababyonalIn_dababyfo.getInstance().addExpValue(3);
            Pers_dababyonalIn_dababyfo.getInstance().setLastFeedbackTime(currentTime);
            Pers_dababyonalIn_dababyfo.getInstance().uploadUserInfo(this::onSubmitSuccess);
        }else onSubmitSuccess();

    }

    private void onSubmitSuccess(){

        runOnUiThread(() -> {

            stopLoading();

            Toast.makeText(this,R.string.feedback_submit_success,Toast.LENGTH_SHORT).show();

        });

        new Thread(() -> {
            try {
                Thread.sleep(1001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            runOnUiThread(this::onBackPressed);
        }).start();
    }

}